package com.le.sarrs.cms;

import com.le.sarrs.util.DateUtil;
import com.le.sarrs.util.HttpToolkit;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class CmsHandler {

    private static final Logger log = Logger.getLogger(CmsHandler.class);
    private static Map<String, JSONObject> data = Collections.synchronizedMap(new HashMap<String, JSONObject>());
    private static final int LEJIAN_MOVIE_ID = 3112;//影视版id测试3112正式2880
    private static final int LEJIAN_NEWS_ID = 8609; //咨询版id8609测试7911正式
    private static final int LEJIAN_HAIWAI_ID = 2000014; //海外版id2000014测试
    private volatile static ScheduledCheckMap scheduledCms;

    private static ScheduledCheckMap rollingRead() {
        if (null == scheduledCms) {
            scheduledCms = new ScheduledCheckMap(5, TimeUnit.MINUTES) {
                @Override
                protected Map<String, JSONObject> query() {
                    readCms();
                    return data;
                }
            };
        }
        return scheduledCms;
    }

    public static void init() {
        readCms();
        rollingRead();
    }

    public static JSONObject getData(String versionType, int type) {
        return type == -1 ? data.get(versionType) : data.get(versionType + "_" + type);
    }

    private static void readCms() {
        if (data != null && data.size() > 0)
            data.clear();
        log.info(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss") + ": cms load data.");
        String url = "http://127.0.0.1/blockNew/get?id=" + LEJIAN_MOVIE_ID; //影视版
        JSONObject result = JSONObject.fromObject(HttpToolkit.sendGet(url));
        data.put("LeJianMovie", processCmsData(result, -1));//全数据(启动和置顶)
        data.put("LeJianMovie_0", processCmsData(result, 0));//启动
        JSONObject jMovie = processCmsData(result, 1);
        data.put("LeJianMovie_1", jMovie);//置顶
        writeFile(jMovie.toString(), "lmovie");


        url = "http://127.0.0.1/blockNew/get?id=" + LEJIAN_NEWS_ID; //咨询版
        JSONObject result1 = JSONObject.fromObject(HttpToolkit.sendGet(url));
        JSONObject jNews = processCmsDataLN(result1);
        data.put("LeJianNews_1", jNews);//置顶
        writeFile(jNews.toString(), "lnews");

        url = "http://127.0.0.1/blockNew/get?id=" + LEJIAN_HAIWAI_ID; //海外版
        JSONObject result2 = JSONObject.fromObject(HttpToolkit.sendGet(url));
        JSONObject jHaiwai = processCmsDataHaiwai(result2);
        data.put("InternationalVideo_1", jHaiwai);//海外置顶
    }

    private static void writeFile(String content, String version) {
        if (StringUtils.isBlank(version))
            return;
        FileWriter fw = null;
        try {
            fw = new FileWriter("/letv/prj/cms/" + version + ".log");
            fw.write(content);

        } catch (IOException e) {
            log.error("write file error.");
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (Exception ignored) {
                }
            }
        }
    }


    private static JSONObject processCmsDataLN(JSONObject result) { //资讯版
        JSONObject json = new JSONObject();
        if (!result.isNullObject() && !result.isEmpty()) {
            Integer ret = (Integer) result.opt("code");
            if (200 == ret) {
                JSONObject tmpData = new JSONObject();
                JSONArray recArray = new JSONArray();
                JSONObject data = result.optJSONObject("data");
                if (!data.isNullObject() && !data.isEmpty()) {
                    JSONArray blockContent = data.optJSONArray("blockContent");
                    if (blockContent != null && blockContent.size() > 0) {
                        for (int i = 0; i < blockContent.size(); i++) {
                            if (adapterLejianNews(blockContent.optJSONObject(i)) != null) {
                                JSONObject tmp = adapterLejianNews(blockContent.optJSONObject(i));
                                if (tmp != null) {
                                    recArray.add(tmp);
                                    break;
                                }
                            }
                        }
                    }
                }
                tmpData.put("records", recArray);
                json.put("data", tmpData);
                json.put("status", "1");
            } else {
                json.put("serverTime", new Date().getTime());
                json.put("status", "201");
                json.put("message", result.opt("msg"));
                log.error("get startImg error.");
            }
        } else {
            json.put("serverTime", new Date().getTime());
            json.put("status", "201");
            json.put("message", "result is empty.");
            log.error("get startImg error.");
        }
        return json;
    }

    private static JSONObject processCmsDataHaiwai(JSONObject result) { //美国版
        JSONObject json = new JSONObject();
        if (!result.isNullObject() && !result.isEmpty()) {
            Integer ret = (Integer) result.opt("code");
            if (200 == ret) {
                JSONObject tmpData = new JSONObject();
                JSONArray recArray = new JSONArray();
                JSONObject data = result.optJSONObject("data");
                if (!data.isNullObject() && !data.isEmpty()) {
                    JSONArray blockContent = data.optJSONArray("blockContent");
                    if (blockContent != null && blockContent.size() > 0) {
                        for (int i = 0; i < blockContent.size(); i++) {
                            if (adapterLejianNews(blockContent.optJSONObject(i)) != null) {
                                JSONObject tmp = adapterLejianHaiwai(blockContent.optJSONObject(i));
                                if (tmp != null) {
                                    recArray.add(tmp);
                                    break;
                                }
                            }
                        }
                    }
                }
                tmpData.put("records", recArray);
                json.put("data", tmpData);
                json.put("status", "1");
            } else {
                json.put("serverTime", new Date().getTime());
                json.put("status", "201");
                json.put("message", result.opt("msg"));
                log.error("get startImg error.");
            }
        } else {
            json.put("serverTime", new Date().getTime());
            json.put("status", "201");
            json.put("message", "result is empty.");
            log.error("get startImg error.");
        }
        return json;
    }


    private static JSONObject processCmsData(JSONObject result, int type) { //影视版
        JSONObject json = new JSONObject();
        if (!result.isNullObject() && !result.isEmpty()) {
            Integer ret = (Integer) result.opt("code");
            if (200 == ret) {
                JSONObject tmpData = new JSONObject();
                JSONArray recArray = new JSONArray();
                JSONObject data = result.optJSONObject("data");
                if (!data.isNullObject() && !data.isEmpty()) {
                    tmpData.put("id", data.opt("id"));
                    tmpData.put("name", data.opt("name"));
                    JSONArray blockContent = data.optJSONArray("blockContent");
                    if (blockContent != null && blockContent.size() > 0) {
                        for (int i = 0; i < blockContent.size(); i++) {
                            if (adapterData(blockContent.optJSONObject(i)) != null) {
                                if (type == -1) {
                                    recArray.add(adapterData(blockContent.optJSONObject(i)));
                                } else {
                                    JSONObject tmp = adapter(blockContent.optJSONObject(i), type);
                                    if (tmp != null)
                                        recArray.add(tmp);
                                }
                            }
                        }
                    }
                }
                tmpData.put("records", recArray);
                json.put("data", tmpData);
                json.put("status", "1");
            } else {
                json.put("serverTime", new Date().getTime());
                json.put("status", "201");
                json.put("message", result.opt("msg"));
                log.error("get startImg error.");
            }
        } else {
            json.put("serverTime", new Date().getTime());
            json.put("status", "201");
            json.put("message", "result is empty.");
            log.error("get startImg error.");
        }
        return json;
    }

    private static JSONObject adapterData(JSONObject obj) {
        JSONObject newObj = null;
        try {
            if (obj == null)
                return null;
            int priority = obj.optInt("priority");
            if (priority >= 1000 && priority <= 1010) { //启动图
                String startTime = "";
                if (!StringUtils.isBlank(obj.optString("startTime"))) {
                    startTime = obj.optString("startTime").replaceAll("：", ":").replaceAll("－", "-");
                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (StringUtils.isNotBlank(startTime) && startTime.contains(DateUtil.getCurrentDate("yyyy-MM-dd"))) { //只出当天启动图
                    newObj = new JSONObject();
                    String name = obj.optString("title");
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(name));
                    newObj.put("shorDesc", obj.opt("shorDesc"));

                    newObj.put("startTime", startTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));
                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("hPic", picList.optString("960x540"));
                    newObj.put("vPic", picList.optString("pic1"));

                    JSONObject extendsJson = obj.optJSONObject("extendJson");
                    newObj.put("extendSubscript", extendsJson.optString("extendSubscript", "244")); //模版默认为A
                    newObj.put("flag", 0);//启动
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    int type = obj.optInt("type");
                    if (type == 1) { //视频
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("key", "101_" + obj.opt("content"));
                    } else if (type == 2) { //专辑
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("key", "110_" + obj.opt("content"));
                    } else if (type == 5) {
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("key", "13_" + obj.opt("content"));
                    } else if (type == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                    }
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("cid", obj.opt("cid"));
                    newObj.put("remark", obj.opt("remark"));
                    //拼接主标题
                    JSONObject tmp = newObj.optJSONObject("info");
                    if (tmp != null) {
                        String category = tmp.optString("cid");
                        if ("2".equals(category) || "5".equals(category)) { //电视剧和动漫
                            int nowp = tmp.optInt("nowEpisodes");
                            int episode = tmp.optInt("episode");
                            if (nowp > 0 && "0".equals(tmp.optString("isEnd"))) {
                                name = name + " | " + "更新至第" + nowp + "集";
                            } else if (episode > 0 && "1".equals(tmp.optString("isEnd"))) {
                                name = name + " | " + episode + "集全";
                            }
                        } else if ("16".equals(category) || "11".equals(category)) { //纪录片和综艺
                            name = name + " | " + formatDate(tmp.containsKey("nowEpisodes") ? tmp.optString("nowEpisodes") : "", "1".equals(tmp.optString("isEnd")));
                        } else if ("1".equals(category)) { //电影
                            String filmTmp = tmp.optString("releaseDate");
                            if (StringUtils.isNotBlank(filmTmp) && filmTmp.length() >= 10) {
                                name += " | " + filmTmp.substring(0, 4) + "年";
                            }
                            double score = tmp.optDouble("score");
                            if (score > 0) {
                                name += " | " + score + "分";
                            }
                        } else { //时长小于15分钟短视频
                            int duration = tmp.optInt("playDuration", -1);
                            if (duration > 0 && duration < 15 * 60) {
                                int minute = duration / 60;
                                int second = duration % 60;
                                name += " | " + String.format("%02d", minute) + "'" + String.format("%02d", second) + "''";
                            }

                        }
                    }
                    newObj.put("titleNew", StringEscapeUtils.unescapeHtml4(name));
                }
            } else if (priority == 1011 || priority == 1012) { //置顶
                String startTime = "";
                String endTime = "";
                if (!StringUtils.isBlank(obj.optString("startTime")) && !StringUtils.isBlank(obj.optString("endTime"))) {
                    startTime = obj.optString("startTime").replaceAll("：", ":").replaceAll("－", "-");
                    endTime = obj.optString("endTime").replaceAll("：", ":").replaceAll("－", "-");

                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                    endTime = DateUtil.getFormatDate(endTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && DateTime.parse(startTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isBeforeNow() && DateTime.parse(endTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isAfterNow()) { //只出置顶时间段内的
                    newObj = new JSONObject();
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(obj.optString("title")));
                    newObj.put("shorDesc", obj.opt("shorDesc"));
                    newObj.put("startTime", startTime);
                    newObj.put("endTime", endTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));

                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("hPic", picList.optString("960x540"));

                    JSONObject extendsJson = obj.optJSONObject("extendJson");
                    newObj.put("extendSubscript", extendsJson.opt("extendSubscript"));
                    newObj.put("flag", 1); //置顶
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    int tmp = obj.optInt("type");
                    if (tmp == 1) {
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("key", "101_" + obj.opt("content"));
                    } else if (tmp == 2) {
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("key", "110_" + obj.opt("content"));
                    } else if (tmp == 5) {
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("key", "13_" + obj.opt("content"));
                    } else if (tmp == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 6) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 3);
                        newObj.put("key", "11_" + obj.opt("content"));
                    }
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("cid", obj.opt("cid"));
                    newObj.put("remark", obj.opt("remark"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("cms data error:" + e.getMessage());
        }
        return newObj;
    }

    private static JSONObject adapter(JSONObject obj, int type) {
        JSONObject newObj = null;
        try {
            if (obj == null)
                return null;
            int priority = obj.optInt("priority");
            if (priority >= 1000 && priority <= 1010 && type == 0) { //启动图
                String startTime = "";
                if (!StringUtils.isBlank(obj.optString("startTime"))) {
                    startTime = obj.optString("startTime").replaceAll("：", ":").replaceAll("－", "-");
                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (StringUtils.isNotBlank(startTime) && startTime.contains(DateUtil.getCurrentDate("yyyy-MM-dd"))) { //只出当天启动图
                    newObj = new JSONObject();
                    String name = obj.optString("title");
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(name));
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    newObj.put("src", handleSrc(obj.optInt("type")));
                    if (obj.optInt("type") == 1) {
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("key", "101_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 2) {
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("key", "110_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 5) {
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("key", "13_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                    }
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("remark", obj.opt("remark"));
                    newObj.put("shorDesc", obj.opt("shorDesc"));

                    newObj.put("startTime", startTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));
                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("hPic", picList.optString("960x540"));
                    newObj.put("vPic", picList.optString("pic1"));

                    JSONObject extendsJson = obj.optJSONObject("extendJson");
                    newObj.put("extendSubscript", extendsJson.optString("extendSubscript", "244"));
                    newObj.put("flag", 0);//启动

                    //拼接主标题
                    JSONObject tmp = newObj.optJSONObject("info");
                    if (tmp != null) {
                        String category = tmp.optString("cid");
                        if ("2".equals(category) || "5".equals(category)) { //电视剧和动漫
                            int nowp = tmp.optInt("nowEpisodes");
                            int episode = tmp.optInt("episode");
                            if (nowp > 0 && "0".equals(tmp.optString("isEnd"))) {
                                name = name + " | " + "更新至第" + nowp + "集";
                            } else if (episode > 0 && "1".equals(tmp.optString("isEnd"))) {
                                name = name + " | " + episode + "集全";
                            }
                        } else if ("16".equals(category) || "11".equals(category)) { //纪录片和综艺
                            name = name + " | " + formatDate(tmp.containsKey("nowEpisodes") ? tmp.optString("nowEpisodes") : "", "1".equals(tmp.optString("isEnd")));
                        } else if ("1".equals(category)) { //电影
                            String filmTmp = tmp.optString("releaseDate");
                            if (StringUtils.isNotBlank(filmTmp) && filmTmp.length() >= 10) {
                                name = name + " | " + filmTmp.substring(0, 4) + "年";
                            }
                            double score = tmp.optDouble("score");
                            if (score > 0) {
                                name += " | " + score + "分";
                            }
                        } else { //时长小于15分钟短视频
                            int duration = tmp.optInt("playDuration", -1);
                            if (duration > 0 && duration < 15 * 60) {
                                int minute = duration / 60;
                                int second = duration % 60;
                                name += " | " + String.format("%02d", minute) + "'" + String.format("%02d", second) + "''";
                            }

                        }
                    }
                    newObj.put("titleNew", StringEscapeUtils.unescapeHtml4(name));
                }
            } else if (type == 1 && (priority == 1011 || priority == 1012)) { //置顶
                String startTime = "";
                String endTime = "";
                if (!StringUtils.isBlank(obj.optString("startTime")) && !StringUtils.isBlank(obj.optString("endTime"))) {
                    startTime = obj.optString("startTime").replaceAll("：", ":").replaceAll("－", "-");
                    endTime = obj.optString("endTime").replaceAll("：", ":").replaceAll("－", "-");
                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                    endTime = DateUtil.getFormatDate(endTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime) && DateTime.parse(startTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isBeforeNow() && DateTime.parse(endTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isAfterNow()) { //只出置顶时间段内的
                    newObj = new JSONObject();
                    if (obj.optInt("type") == 1) {
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("key", "101_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 2) {
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("key", "110_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 5) {
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("key", "13_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                    } else if (obj.optInt("type") == 6) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 3);
                        newObj.put("key", "11_" + obj.opt("content"));
                    }
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(obj.optString("title")));
                    newObj.put("shorDesc", obj.opt("shorDesc"));
                    newObj.put("startTime", startTime);
                    newObj.put("endTime", endTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    newObj.put("src", handleSrc(obj.optInt("type")));
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("remark", obj.opt("remark"));

                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("pic", picList.optString("960x540"));

                    JSONObject extendsJson = obj.optJSONObject("extendJson");
                    newObj.put("extendSubscript", extendsJson.opt("extendSubscript"));
                    newObj.put("flag", 1); //置顶
                }
            }
        } catch (Exception e) {
            log.error("cms data error:", e);
        }
        return newObj;
    }

    private static String handleType(int type) {
        String result = "";
        //1、2、8、10、11
        if (type == 1 || type == 2 || type == 8 || type == 10 || type == 11) {
            result = "wv";
        } else if (type == 5) {
            result = "zt";
        } else if (type == 3 || type == 6 || type == 7 || type == 16) { // 3、6、7、16
            result = "live2";
        }
        return result;
    }

    private static String handleSrc(int type) {
        String result = "1";
        //8、10、11外网
        if (type == 8 || type == 10 || type == 11) {
            result = "2";
        }
        return result;
    }

    private static JSONObject handleVideo(JSONObject video) {
        JSONObject result = new JSONObject();
        if (video == null)
            return result;
        result.put("cid", video.opt("cid"));
        result.put("category", video.opt("chanName"));
        result.put("episode", video.opt("episode"));
        result.put("nowEpisodes", video.opt("nowEpisodes"));
        result.put("releaseDate", video.opt("releaseDate"));
        result.put("score", video.opt("score"));
        result.put("playDuration", video.opt("duration"));
        result.put("vtypeFlag", video.opt("vtypeFlag"));

        return result;
    }

    private static JSONObject handleAlbum(JSONObject album) {
        JSONObject result = new JSONObject();
        if (album == null)
            return result;
        JSONObject category = album.optJSONObject("category");
        if (!category.isNullObject() && !category.isEmpty() && category.keys().hasNext()) {
            String cid = (String) category.keys().next();
            result.put("cid", cid);
            result.put("category", category.opt(cid));
        }
        result.put("episode", album.opt("episode"));
        result.put("nowEpisodes", album.opt("nowEpisodes"));
        result.put("isEnd", album.opt("isEnd"));
        result.put("releaseDate", album.opt("releaseDate"));
        result.put("score", album.opt("score"));
        result.put("playDuration", album.opt("duration"));

        return result;
    }

    private static JSONObject adapterLejianNews(JSONObject obj) { //资讯版置顶
        JSONObject newObj = null;
        try {
            if (obj == null)
                return null;
            int priority = obj.optInt("priority");
            if (priority == 1011) { //置顶
                String startTime = "";
                String endTime = "";
                if (!StringUtils.isBlank(obj.optString("startTime")) && !StringUtils.isBlank(obj.optString("endTime"))) {
                    startTime = obj.optString("startTime").replaceAll("：", ":").replaceAll("－", "-");
                    endTime = obj.optString("endTime").replaceAll("：", ":").replaceAll("－", "-");

                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                    endTime = DateUtil.getFormatDate(endTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime) && DateTime.parse(startTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isBeforeNow() && DateTime.parse(endTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isAfterNow()) { //只出置顶时间段内的
                    newObj = new JSONObject();
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(obj.optString("title")));
                    newObj.put("shorDesc", obj.opt("shorDesc"));
                    newObj.put("startTime", startTime);
                    newObj.put("endTime", endTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    newObj.put("src", handleSrc(obj.optInt("type")));
                    if (obj.optInt("type") == 1) {
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("key", "101_" + obj.opt("content")); //4_
                        newObj.put("aid", "");
                        newObj.put("tid", "");
                    } else if (obj.optInt("type") == 2) {
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("key", "110_" + obj.opt("content")); //1_
                        newObj.put("vid", "");
                        newObj.put("tid", "");
                    } else if (obj.optInt("type") == 5) {
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("key", "13_" + obj.opt("content"));
                        newObj.put("aid", "");
                        newObj.put("vid", "");
                    } else if (obj.optInt("type") == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                        newObj.put("aid", "");
                        newObj.put("tid", "");
                        newObj.put("vid", "");
                    } else {
                        newObj.put("key", obj.opt("content"));
                    }
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("remark", obj.opt("remark"));

                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("pic", picList.optString("960x540"));

                    JSONObject extendsJson = obj.optJSONObject("extendJson");
                    newObj.put("extendSubscript", extendsJson.opt("extendSubscript"));
                    newObj.put("flag", 1); //置顶
                }
            }
        } catch (Exception e) {
            log.error("cms data error:" + e.getMessage());
        }
        return newObj;
    }

    private static JSONObject adapterLejianHaiwai(JSONObject obj) { //海外版置顶
        JSONObject newObj = null;
        try {
            if (obj == null)
                return null;
            int priority = obj.optInt("priority");
            if (priority == 1011 || priority == 1012 || priority == 1103) { //置顶
                String startTime = "";
                String endTime = "";

                JSONObject extendsJson = obj.optJSONObject("extendJson");

                if (!StringUtils.isBlank(extendsJson.optString("liveBeginTime")) && !StringUtils.isBlank(extendsJson.optString("liveEndTime"))) {
                    startTime = extendsJson.optString("liveBeginTime").replaceAll("：", ":").replaceAll("－", "-");
                    endTime = extendsJson.optString("liveEndTime").replaceAll("：", ":").replaceAll("－", "-");

                    startTime = DateUtil.getFormatDate(startTime, "yyyy-MM-dd HH:mm:ss");
                    endTime = DateUtil.getFormatDate(endTime, "yyyy-MM-dd HH:mm:ss");
                }
                if (!StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime) && DateTime.parse(startTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isBeforeNow() && DateTime.parse(endTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).isAfterNow()) { //只出置顶时间段内的
                    newObj = new JSONObject();
                    newObj.put("title", StringEscapeUtils.unescapeHtml4(obj.optString("title")));
                    newObj.put("shorDesc", obj.opt("shorDesc"));
                    newObj.put("startTime", startTime);
                    newObj.put("endTime", endTime);
                    newObj.put("priority", obj.opt("priority"));
                    newObj.put("url", obj.opt("url"));
                    newObj.put("subTitle", obj.opt("subTitle"));
                    newObj.put("bid", obj.opt("bid"));
                    newObj.put("type", handleType(obj.optInt("type")));
                    newObj.put("src", handleSrc(obj.optInt("type")));
                    if (obj.optInt("type") == 1) {
                        newObj.put("info", handleVideo(obj.optJSONObject("video")));
                        newObj.put("vid", obj.opt("content"));
                        newObj.put("key", "101_" + obj.opt("content")); //4_
                        newObj.put("aid", "");
                        newObj.put("tid", "");
                    } else if (obj.optInt("type") == 2) {
                        newObj.put("info", handleAlbum(obj.optJSONObject("album")));
                        newObj.put("aid", obj.opt("content"));
                        newObj.put("key", "110_" + obj.opt("content")); //1_
                        newObj.put("vid", "");
                        newObj.put("tid", "");
                    } else if (obj.optInt("type") == 5) {
                        newObj.put("info", obj.opt("kztInfo"));
                        newObj.put("tid", obj.opt("content"));
                        newObj.put("key", "13_" + obj.opt("content"));
                        newObj.put("aid", "");
                        newObj.put("vid", "");
                    } else if (obj.optInt("type") == 3) {
                        newObj.put("liveId", obj.opt("content"));
                        newObj.put("liveType", 2);
                        newObj.put("key", "11_" + obj.opt("content"));
                        newObj.put("aid", "");
                        newObj.put("tid", "");
                        newObj.put("vid", "");
                    } else {
                        newObj.put("key", obj.opt("content"));
                    }
                    newObj.put("ctime", obj.opt("ctime"));
                    newObj.put("mtime", obj.opt("mtime"));
                    newObj.put("id", obj.opt("id"));
                    newObj.put("remark", obj.opt("remark"));

                    JSONObject picList = obj.optJSONObject("picList");
                    newObj.put("pic", picList.optString("960x540"));


                    newObj.put("extendSubscript", extendsJson.opt("extendSubscript"));
                    newObj.put("flag", 1); //置顶
                }
            }
        } catch (Exception e) {
            log.error("cms data error:" + e.getMessage());
        }
        return newObj;
    }


    //日期格式转换 - 20150323转为2015-03-23
    private static String formatDate(String date, boolean isEnd) {
        String fDate = "";
        if (StringUtils.isBlank(date))
            return fDate;
        if (!isEnd && 8 == date.length()) {
            fDate = "更新至" + date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + "期 ";
        } else if (8 == date.length()) {
            fDate = date.substring(0, 4) + "年" + date.substring(4, 6) + "月" + date.substring(6, 8) + "日期 ";
        } else {
            if (isEnd) {
                fDate = "" + date + "集全";
            } else {
                fDate = "更新至第" + date + "集";
            }
        }
        return fDate;
    }
}
